﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace YArchitech.HVAC
{
	public class FloorSystem
	{
		public FloorSystem(int floorNum)
		{
			this.index = floorNum;
			this.indoors = new List<IndoorSystem>();
		}

		public FloorSystem()
		{
			this.indoors = new List<IndoorSystem>();
		}

		public int Index
		{
			get
			{
				return this.index;
			}
			set
			{
				this.index = value;
			}
		}

		public string Name
		{
			get
			{
				return "楼层" + this.index;
			}
		}

		public double Height
		{
			get
			{
				return this.dHeight;
			}
			set
			{
				this.dHeight = value;
			}
		}

		public double Resistance
		{
			get
			{
				return this.resistance;
			}
		}

		public double PressActual
		{
			get
			{
				return this.pressActual;
			}
			set
			{
				this.pressActual = value;
			}
		}

		public double GravityPress
		{
			get
			{
				return this.gravityPress;
			}
		}

		public double RateUnbalance { get; set; }

		public HyCalcPipe SupplyPipe { get; set; }

		public HyCalcPipe BackPipe { get; set; }

		public IList<IndoorSystem> InDoorSystem
		{
			get
			{
				return this.indoors;
			}
		}

		public int WorstIndex
		{
			get
			{
				return this.GetWorstIndoorIndex();
			}
		}

		public void Add(IndoorSystem indoor)
		{
			this.indoors.Add(indoor);
		}

		public IndoorSystem GetAt(int indoorIndex)
		{
			return (from indoor in this.indoors
			where indoor.Index == indoorIndex
			select indoor).First<IndoorSystem>();
		}

		public Dictionary<string, double> CalcUnbalance()
		{
			Dictionary<string, double> dictionary = new Dictionary<string, double>();
			double num = (from indoor in this.indoors
			select indoor.Resistance).Max();
			foreach (IndoorSystem indoorSystem in this.indoors)
			{
				indoorSystem.CalaUnbalance();
				double num2 = Math.Abs((indoorSystem.Resistance - num) / num);
				indoorSystem.RateUnbalance = num2;
				dictionary.Add(indoorSystem.Name, num2);
				Log.Info(new string[]
				{
					string.Format("System {0} Resistance = {1} Unbalance={2}", indoorSystem.Name, indoorSystem.Resistance, num2)
				});
			}
			return dictionary;
		}

		private int GetWorstIndoorIndex()
		{
			this.worstIndex = (from loop in this.indoors
			where loop.Resistance == (from indoor in this.indoors
			select indoor.Resistance).Max()
			select loop.Index).First<int>();
			return this.worstIndex;
		}

		public double CalcResistance()
		{
			IndoorLoop indoorLoop = this.GetAt(1).Loops[0];
			this.gravityPress = 6.540327 * this.dHeight * (indoorLoop.BackPipes.At(1).Density - indoorLoop.SupplyPipes.At(1).Density);
			double num = (from indoor in this.indoors
			select indoor.CalcResistance()).Max();
			this.resistance = num + this.gravityPress;
			Log.Info(new string[]
			{
				this.Name + ": resistance = " + this.resistance
			});
			return this.resistance;
		}

		public TreeNode ToTreeNode()
		{
			TreeNode treeNode = new TreeNode(this.Name);
			treeNode.Tag = this;
			if (this.BackPipe != null && this.SupplyPipe != null)
			{
				TreeNode treeNode2 = new TreeNode("供水干管");
				treeNode2.Tag = this.SupplyPipe;
				TreeNode treeNode3 = new TreeNode("回水干管");
				treeNode3.Tag = this.BackPipe;
				treeNode.Nodes.AddRange(new TreeNode[]
				{
					treeNode2,
					treeNode3
				});
			}
			treeNode.Nodes.AddRange((from indoor in this.indoors
			select indoor.ToTreeNode()).ToArray<TreeNode>());
			return treeNode;
		}

		public void UpDate()
		{
			if (this.SupplyPipe != null)
			{
				this.SupplyPipe.GetAllParam();
				this.BackPipe.GetAllParam();
			}
			this.indoors.ForEach(delegate(IndoorSystem indoor)
			{
				indoor.UpDate();
			});
		}

		public void BackRevit()
		{
			if (this.SupplyPipe != null)
			{
				this.SupplyPipe.BackRevit();
				this.BackPipe.BackRevit();
			}
			this.indoors.ForEach(delegate(IndoorSystem indoor)
			{
				indoor.BackRevit();
			});
		}

		private int index = 1;

		private IList<IndoorSystem> indoors;

		private double dHeight;

		private int worstIndex = 1;

		private double resistance;

		private double pressActual;

		private double gravityPress;
	}
}
